<?php

namespace app\service;

use app\exception\ErrnoPlus;
use app\model\TutorialVideoModel;
use share\exception\ApiException;
use share\service\BaseModelService;
use share\utils\Helper;

class TutorialVideoService extends BaseModelService
{

    public static function getModel()
    {
        return new TutorialVideoModel;
    }
   
    public static function getAllByModule($param){
        $videos =  self::getAllByFilter([
            'system' => $param['system'] === Helper::CLIENT_TYPE_OF_IOS ? 'iOS' : 'Android',
            'vendor' => $param['vendor'],
            'model_id' => $param['moduleId'],
        ]);
        if(empty($videos)){
            return ;
        }
        $describe = DescribeInfoService::getAllByFilter([
            'language' => $param['language'],
        ]);
        $res = [];
        foreach($videos as $video){
            $titleId = $video['title_id'];
            $desc = Helper::filterOne($describe,['describe_id' => $titleId]);
            if($desc){
                $res[] = [
                    'title' => $desc['describe_name'],
                    'uri' => $video['url'],
                    'picture' => $video['default_picture'],
                ];  
            }
        }
        return $res;
    }

    public static function searchByTitle($param){
        $videos =  self::getAllByFilter([
            'system' => $param['system'] === Helper::CLIENT_TYPE_OF_IOS ? 'iOS' : 'Android',
            'vendor' => $param['vendor'],
        ]);
        if(empty($videos)){
            return ;
        }
        $describe = DescribeInfoService::getAllByFilter([
            'language' => $param['language'],
            'describe_name' => ["like","%".$param['title']."%"]
        ]);
        $res = [];
        foreach($videos as $video){
            $titleId = $video['title_id'];
            $desc = Helper::filterOne($describe,['describe_id' => $titleId]);
            if($desc){
                $res[] = [
                    'title' => $desc['describe_name'],
                    'uri' => $video['url'],
                    'picture' => $video['default_picture'],
                ];  
            }
        }
        return $res;
    }

    public static function addVideo($param){
        $module = ModuleInfoService::getBase($param['modelId']);
        if(empty($module)){
            throw new ApiException("module is not exist",ErrnoPlus::COMMON_ERROR);
        }
        $titleId = Helper::getUnique();
        DescribeInfoService::addDescribe([
            'describe_id' => $titleId,
            'language' => $param['language'],
            'describe_name' => $param['titleName'],
        ]);
        unset($param['language']);
        unset($param['titleName']);
        $param['titleId'] = $titleId;
        $item = Helper::arrayCamelKeyToUnderscore($param);
        self::add($item);
    }

    public static function updateVideo($param){
        $id =  $param['id'];
        unset($param['id']);
        if(isset($param['modelId'])){
            $module = ModuleInfoService::getBase($param['modelId']);
            if(empty($module)){
                throw new ApiException("module is not exist",ErrnoPlus::COMMON_ERROR);
            }
        }
        $origin = self::getBase($id);
        if(empty($origin)){
            throw new ApiException("video info is not exist",ErrnoPlus::COMMON_ERROR);
        }
        $item = Helper::arrayCamelKeyToUnderscore($param);
        $update = [];
        foreach($item as $key => $val){
            if($val != $origin[$key]){
                $update[$key] = $val;
            }
        }
        if(count($update) > 0){
            self::update($id,$update);
        }
    }

    public static function delVideo($id){
        $origin = self::getBase($id);
        if(empty($origin)){
            throw new ApiException("video info is not exist",ErrnoPlus::COMMON_ERROR);
        }
        DescribeInfoService::delByFilter([
            'describe_id' =>  $origin['title_id'],
        ]);
        self::del($id);
    }

    public static function buildCondition($filter)
    {
        $res = [];
        if(isset($filter['vendor']) && !empty($filter['vendor'])){
            $res['vendor'] = $filter['vendor'];
        }
        return $res;
        
    }

    public static function getListInfo($filter,$pageInfo,$order = null){
        $condition = static::buildCondition($filter);
        $listData = static::getPageList($condition,$pageInfo['page'],$pageInfo['size']);
        if(count($listData) < 1){
            return $listData;            
        }
        $moduleIds = Helper::arraySmartUnique(array_column($listData,'model_id'));
        $moduleInfos = ModuleInfoService::getAll($moduleIds);
        $titleDescIds = Helper::arraySmartUnique(array_column($listData,'title_id'));
        $moduleDescIds = Helper::arraySmartUnique(array_column($moduleInfos,'describe_id'));
        $descIds = array_merge($titleDescIds,$moduleDescIds);
        $descFilter = [
            'describe_id' => ['in',$descIds],
        ];
        if(isset($filter['language'])){
            $descFilter['language'] = $filter['language'];
        }
        $descInfos = DescribeInfoService::getAllByFilter($descFilter);
        foreach($listData as &$data){
            $module = Helper::filterOne($moduleInfos,['id'=>$data['model_id']]);
            $data['modelName'] = '';
            $data['modelDescribeId'] = '';
            if($module){
                $data['modelDescribeId']  = $module['describe_id'];
                $moduleDesc = Helper::filterOne($descInfos,['describe_id'=>$module['describe_id']]);
                if($moduleDesc){
                    $data['modelName'] = $moduleDesc['describe_name'];
                }
            }
            $data['titleDescribeId'] = $data['title_id'];
            $titleDesc = Helper::filterOne($descInfos,['describe_id'=>$data['title_id']]);
            if($titleDesc){
                $data['titleName'] = $titleDesc['describe_name'];
            }
        }
        return $listData;
    }


}
